package com.zhsj.OkeHomeConsumption.ui.activity.entering;

import android.app.Activity;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.utils.HttpDown;

import java.io.File;


/**
 * @author zhangpeisen
 * @ClassName: ShowSignCompactsFragment
 * @Description: TODO 合同签署
 * @date 2016年11月14日 下午3:01:55
 */
public class ShowSignCompactsFragment extends MyActivity {

    // 左标题
    private TextView topbar_textview_leftitle = null;
    // 主标题
    private TextView topbar_textview_title = null;
    /**
     * @Fields neterror_relative : TODO
     */
    private RelativeLayout neterror_relative = null;
    /**
     * @Fields btn_loadingrefresh : TODO
     */
    private Button btn_loadingrefresh = null;

    private String webview_url;
    private int type = -1;
    private String mTag = "";
    private PDFView pdfView;
    private HttpDown http;
    private String msg;

    private SubsamplingScaleImageView img ;





    protected int getLayoutId() {
        // TODO Auto-generated method stub
        return R.layout.showsigncompacts_webviewlayout;
    }

    @Override
    protected int getTitleId() {
        return 0;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type",-1);
        webview_url = getIntent().getStringExtra("web_url");
        img = (SubsamplingScaleImageView)findViewById(R.id.img);
        topbar_textview_leftitle = findViewById(R.id.topbar_textview_leftitle);
        topbar_textview_title = findViewById(R.id.topbar_textview_title);
        topbar_textview_title.setText("电子合同");
        topbar_textview_leftitle.setVisibility(View.VISIBLE);
        pdfView = (PDFView) findViewById(R.id.pdfView);

        String extension = "";
        int i = webview_url.lastIndexOf('.');
        if (i > 0) {
            extension = webview_url.substring(i+1);
        }

        if("pdf".equals(extension)){
            Uri uri = null;
            if (type == 1) {
                pdfView.setVisibility(View.GONE);
            } else {
                final String s = webview_url.substring(webview_url.lastIndexOf("/") + 1, webview_url.length());
                final String pdfName = Environment.getExternalStorageDirectory() +
                        "/download/";
                File file1 = new File(pdfName + s);
                if (!file1.exists()) {
                    new Thread(
                            new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    http = new HttpDown();
                                    //获得SD卡路径
                                    String str = webview_url;
                                    msg = http.download(str, pdfName, s);
                                    handler.sendEmptyMessage(0);
                                }
                            }
                    ).start();
                }else{
                    loaduri(pdfName,s,uri);
                }
            }
        }else{
            img.setVisibility(View.VISIBLE);
            //下载图片保存到本地  长图加载
            Glide.with(this)
                    .load(webview_url).downloadOnly(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
                    // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                    img.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(1.0F, new PointF(0, 0), 0));
                }

               });
        }


    }

    @Override
    protected void initData() {
        topbar_textview_leftitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private Handler handler = new Handler() {
        private int process = 0;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Uri uri = null;
            final String s = webview_url.substring(webview_url.lastIndexOf("/") + 1, webview_url.length());
            final String pdfName = Environment.getExternalStorageDirectory() +
                    "/download/";
            loaduri(pdfName,s,uri);
        }
    };
    private void loaduri(String pdfName,String s, Uri uri ){
        File file = new File(pdfName, s);
        uri = Uri.fromFile(file);
        pdfView.setVisibility(View.VISIBLE);
        pdfView.fromFile(file)
                //是否允许翻页，默认是允许翻页
                .enableSwipe(true)
                //pdf文档翻页是否是垂直翻页，默认是左右滑动翻页
                .swipeHorizontal(true)
                .defaultPage(0)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int page, int pageCount) {
                        // 当用户在翻页时候将回调。
                        Toast.makeText(getApplicationContext(), (page+1) + " / " + pageCount, Toast.LENGTH_SHORT).show();
                    }
                }).load();

    }

}
